﻿using System.ComponentModel.Composition;
using System.Linq;
using System.Web.Mvc;
using Gestion.Common.Extensions;
using GestionSaludOcupacional.Infrastructure.Mapping;
using GestionSaludOcupacional.Infrastructure.Services;
using GestionSaludOcupacional.Portal.App_Start;
using GestionSaludOcupacional.Portal.Base.ViewModels;
using System;

namespace GestionSaludOcupacional.Portal.Controllers
{
    public class RegistroController : Controller
    {
        [Import]
        public Facade Facade;

        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        [OutputCache(Duration = 0)]
        public ActionResult FormJs(string page)
        {
            object data = null;
            var values = new object();
            switch (page)
            {
                case "Crear":
                    data = new IndividuoViewModel();
                    break;
                case "Editar":
                    var id = Request["idIndividuo"].SafeParse(0);
                    data = ObtenerIndividuo(id);
                    break;
            }
            var result = new { data, values };
            return Json(result, JsonRequestBehavior.AllowGet);
        }


        public ActionResult MostrarExamen(int id, int individuo, string tipo)
        {
            if (tipo == "Vision")
                return RedirectToAction("Vision", "Examen", new { id = individuo, key = id });
            if (tipo == "Audicion")
                return RedirectToAction("Audicion", "Examen", new { id = individuo, key = id });
            if (tipo == "Psicologia")
                return RedirectToAction("Psicologia", "Examen", new { id = individuo, key = id });
            if (tipo == "Psicosensometria")
                return RedirectToAction("Psicosensometrico", "Examen", new { id = individuo, key = id });
            if (tipo == "Fisico")
                return RedirectToAction("Index", "Historia", new { id = individuo, citaId = 0, key = id });
            return HttpNotFound();
        }

        public JsonResult ListaExamenes(int id)
        {
            var lista = Facade.ObtenerListaExamenes(id);
            return Json(new { lista }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult ObtenerIndividuos(string id, string apellido)
        {
            var data = Facade.GetIndividuos(id, apellido);
            return Json(new { data }, JsonRequestBehavior.AllowGet);
        }

        private IndividuoViewModel ObtenerIndividuo(int id)
        {
            return Facade.MappingService.GetViewModel(Facade.HealthService.ObtenerIndividuo(id));
        }

        public JsonResult ObtenerIndividuoDetalle(int id)
        {
            var schema = Facade.MappingService.GetViewModelDetail(Facade.HealthService.ObtenerIndividuo(id));
            if (schema != null)
            {
                if (!string.IsNullOrWhiteSpace(schema.FechaNacimiento))
                    schema.FechaNacimiento = DateTime.Parse(schema.FechaNacimiento).ToShortDateString();
            }
            return Json(new { schema }, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Guardar(IndividuoViewModel individuoViewModel)
        {
            // Validation Model
            if (ModelState.IsValid)
            {
                var isValid = Facade.GuardarIndividuo(individuoViewModel);
                return Json(new { isValid });
            }
            return Json(new { isValid = false, message = ModelState.GetErrors() }, JsonRequestBehavior.AllowGet);
        }
    }
}
